package code;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Class that provides the TargetCode
 * @author sebastian
 *
 */
public class TargetCode {
	private List<Code> mainCodeList = null;
	private Map<Integer, List<Code>> methodToCode = null;
	private Map<Integer, Integer> methodToArgumentNumber = null;
	
	public TargetCode() {
		mainCodeList = new ArrayList<Code>();
		methodToCode = new HashMap<Integer, List<Code>>();
		methodToArgumentNumber = new HashMap<Integer, Integer>();
	}
	
	public void addMethodCode(int methodLocalVar, int argumentNumber, CodeList code) {
		this.methodToArgumentNumber.put(methodLocalVar, argumentNumber);
		this.methodToCode.put(methodLocalVar, code.getCodeAsList());
	}
	
	public void putMainCode(CodeList code) {
		this.mainCodeList = code.getCodeAsList();
	}
	
	public String toSOVMLanguage() {
		String result = "";
		for (Integer i : methodToCode.keySet()) {
			result += "Method " + i + " " + methodToArgumentNumber.get(i) + " :\n";
			for (Code c : methodToCode.get(i)) {
				result += c.toString();
			}
			result += ";\n";
		}
		result += "Main :\n";
		for (Code c : mainCodeList) {
			result += c.toString();
		}
		result += ";\n";
		return result;
	}
}
